<?php namespace App\Api\Http\ApiControllers;

use App\Exceptions\OtherException;
use App\Repositories\Item\CompanyRepositoryInterface;
use App\Repositories\Tag\TagRepositoryInterface;
use Illuminate\Http\Request;

class TagController extends Controller
{
    public function storeItemTag(Request $request)
    {
        $user_id = access()->id();
        $name = $request->get('name');
        throw_if(mb_strlen($name) < 2 || mb_strlen($name) > 10, new OtherException('标签不能超过10个字'));

        /** @var TagRepositoryInterface $tag_repository */
        $tag_repository = app(TagRepositoryInterface::class);
        $tag = $tag_repository->store($user_id, $name);

        return response()->json($tag->only('id', 'name'));
    }
}
